package org.jeecgframework.web.system.service.impl;

import net.lahuobao.finance.entity.account.AccountEntity;
import net.lahuobao.finance.entity.account.EAccountType;
import net.lahuobao.finance.entity.depart.CargoOwnerUser;
import net.lahuobao.finance.entity.depart.MallEcardaccount;
import net.lahuobao.finance.entity.depart.User;
import net.lahuobao.finance.entity.organizationEntity.OrganizationEntity;
import net.lahuobao.finance.entity.reccode.ReccodeEntity;
import net.lahuobao.finance.service.reccode.ReccodeServiceI;
import org.jeecgframework.web.system.manager.ClientManager;
import org.jeecgframework.web.system.pojo.base.Client;
import org.jeecgframework.web.system.pojo.base.TSDepart;
import org.jeecgframework.web.system.pojo.base.TSRoleUser;
import org.jeecgframework.web.system.pojo.base.TSUser;
import org.jeecgframework.web.system.service.UserService;

import org.hibernate.Criteria;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.jeecgframework.core.common.service.impl.CommonServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * 
 * @author  张代浩
 *
 */
@Service("userService")
@Transactional
public class UserServiceImpl extends CommonServiceImpl implements UserService {

	@Autowired
	private ReccodeServiceI reccodeService;

	public TSUser checkUserExits(TSUser user){
		return this.commonDao.getUserByUserIdAndUserNameExits(user);
	}
	public String getUserRole(TSUser user){
		return this.commonDao.getUserRole(user);
	}
	
	public void pwdInit(TSUser user,String newPwd) {
			this.commonDao.pwdInit(user,newPwd);
	}
	
	public int getUsersOfThisRole(String id) {
		Criteria criteria = getSession().createCriteria(TSRoleUser.class);
		criteria.add(Restrictions.eq("TSRole.id", id));
		int allCounts = ((Long) criteria.setProjection(
				Projections.rowCount()).uniqueResult()).intValue();
		return allCounts;
	}

	public <T> Serializable saveDepart(T entity,String sessionId)  {
		TSDepart depart = (TSDepart) entity;
		super.save(entity);

		//推荐码部分
		String rec_code = ""+(1+reccodeService.getMaxRecCode());
		if(rec_code.length() == 1){rec_code = "00000"+rec_code;}
		if(rec_code.length() == 2){rec_code = "0000"+rec_code;}
		if(rec_code.length() == 3){rec_code = "000"+rec_code;}
		if(rec_code.length() == 4){rec_code = "00"+rec_code;}
		if(rec_code.length() == 5){rec_code = "0"+rec_code;}
		ReccodeEntity reccodeEntity = new ReccodeEntity();
		reccodeEntity.setCreatetime(new Date());
		reccodeEntity.setHolder(depart.getId());
		reccodeEntity.setRecCode(rec_code);
		reccodeEntity.setRemarks("添加部门生成推荐码");
		reccodeEntity.setHoldertype(0);
		commonDao.save(reccodeEntity);



		//添加一个组织信息
		OrganizationEntity organization = new OrganizationEntity();
		organization.setOrganizationName(depart.getDepartname());
		organization.setValidateState(2);
		commonDao.save(organization);
		long organization_id = organization.getOrganizationId();

		//User、mall_ecardaccount
		//E卡账户
		User user = new User();
		user.setUserName(depart.getDepartname());
		user.setFullName(depart.getDepartname());
		user.setPassword("f379eaf3c831b04de153469d1bec345e");//666666
		user.setRegDatetime(new Date());
		user.setState(1);
		user.setValidateState(2);
		user.setType(1);
		commonDao.save(user);

		MallEcardaccount ecardaccount = new MallEcardaccount();
		ecardaccount.setUserId(user.getUserId());
		ecardaccount.setEcardBalance(BigDecimal.ZERO);
		commonDao.save(ecardaccount);

		CargoOwnerUser cargoOwnerUser = new CargoOwnerUser();
		cargoOwnerUser.setUserId(user.getUserId());
		cargoOwnerUser.setOrganizationId(organization.getOrganizationId());
		commonDao.save(cargoOwnerUser);


		//添加当前账户信息到账户表
		Client clientOld = ClientManager.getInstance().getClient(sessionId);
		AccountEntity accountEntity = new AccountEntity();
		
		accountEntity.setTotal(BigDecimal.ZERO);
		accountEntity.setFrozen(BigDecimal.ZERO);
		accountEntity.setUsable(BigDecimal.ZERO);
		accountEntity.setOperateTime(new Date());
		accountEntity.setOperateId(10000);
		accountEntity.setAboutId(depart.getId());
		accountEntity.setOrganizationId(organization_id);
		accountEntity.setAccountType(EAccountType.Organization.getValue());
		accountEntity.setBankAccountName("组织机构账户");
		commonDao.save(accountEntity);

		return depart;
	}




}
